package com.shuyun.controller;

import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
import com.netflix.hystrix.strategy.HystrixPlugins;
import com.netflix.hystrix.strategy.properties.HystrixDynamicProperties;
import com.netflix.hystrix.strategy.properties.HystrixDynamicProperty;
import com.shuyun.hystrix.command.CommandDynamicProp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: shuai.chen@shuyun.com
 * @date: 2023/1/18 2:41 PM
 */
@RequestMapping("demo")
@RestController
public class DemoController {

    @GetMapping("/properties-from-dynamic")
    public Map<String, String> getPropertiesFromDynamic() {
        Map<String, String> properties = new HashMap<>();

        HystrixDynamicProperties dynamicProperties = HystrixPlugins.getInstance().getDynamicProperties();
        HystrixDynamicProperty<String> nameProperty = HystrixDynamicProperties.Util.getProperty(dynamicProperties, "name", "not found", String.class);

        // why  没读到最新值
        properties.put(nameProperty.getName(), nameProperty.get());
        return properties;
    }

    @GetMapping("commandDynamicProp")
    public String commandDynamicProp() {
        return new CommandDynamicProp("sjyj", "shuyun.member.query").execute();
    }

}
